from selenium import webdriver
from Common.Decorators import singleton
from selenium.webdriver.support.ui import WebDriverWait
from inspect import getmembers, isclass
from Config.RunConfig import RunConfig, Options


AVAILABLE_DRIVERS = dict(getmembers(webdriver, lambda x: isclass(x) and issubclass(x, webdriver.Remote)))


@singleton
class DRIVER(object):
    def __init__(self, *args, **kwargs):
        object.__init__(self)
        self._driverImpl = AVAILABLE_DRIVERS[RunConfig().get(Options.AirBackup.browser)](*args, **kwargs)
        self._driverImpl.maximize_window()

    def actionChains(self):
        return webdriver.ActionChains(self._driverImpl)

    def waitFor(self, callback, timeout, message='', ignored_exceptions=None):
        waitDriver = WebDriverWait(self._driverImpl, timeout, ignored_exceptions=ignored_exceptions)
        return waitDriver.until(callback, message)

    def browser(self):
        info = self._driverImpl.capabilities
        return "%s %s" % (info['browserName'], info['version'])

    def __getattr__(self, item):
        return getattr(self._driverImpl, item)
